package com.css.apps.base.recycle.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.recycle.model.RTrash;
import com.css.apps.base.recycle.model.RTrashContent;
import com.css.apps.base.recycle.service.ITrashBizHandler;
import com.css.common.PluginBus;
import com.css.common.exception.ExceptionFactory;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * <p>还原回收站记录</p>
 * @author CSS
 *
 */
@SuppressWarnings("all")
public class RecoverRecycle extends AdminAction {
	
	private static Log log = LogFactory.getLog(RecoverRecycle.class);
	private final String funcid = "ACL_RECOVERRECYCLE";
	//回收站记录entityId集合
	private String ids;
 
	public RecoverRecycle() {
		setFuncid(funcid);
	}

	public String adminGo() {
		TransactionCache tx = null;
		try{
			if(StringHelper.isEmpty(ids)){
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));	
				return Action.ERROR;		
			}
			List<String> lst = StringHelper.strToList(ids);
			List<RTrash> itemLst = QueryCache.idToObj(RTrash.class, lst);
			//实例化tx
			tx = new TransactionCache();
			String recordId = null;
			for (RTrash item : itemLst) {
				recordId = item.getRecordId();
				
				ITrashBizHandler bizHandler = PluginBus.getPlugin(ITrashBizHandler.class, item.getFuncId());
				if (bizHandler != null) {
					if (!bizHandler.recoverEntity(tx, recordId)) {
						throw ExceptionFactory.makeBaseException(Messages.getString("systemMsg.exception"));
					}
				}
				/*
				String draftId = DraftService.getDraft(recordId);
				SDraft draft = QueryCache.get(SDraft.class, draftId);
				draft.setDeleteFlag("1");
				tx.update(draft);
				//修改状态
				if (item.getFuncId().equals(Constants.SYS_FW)) {
					FwForm fwForm = QueryCache.get(FwForm.class, recordId);
					fwForm.setDeleteFlag("1");
					tx.update(fwForm);
				} else if (item.getFuncId().equals(Constants.SYS_SW)) {
					SwForm swForm = QueryCache.get(SwForm.class, recordId);
					swForm.setDeleteFlag("1");
					tx.update(swForm);
				} else if (item.getFuncId().equals(Constants.SYS_DB)) {
					Supervise supervise = QueryCache.get(Supervise.class, recordId);
					supervise.setDeleteFlag("1");
					tx.update(supervise);					
				}
				*/
				//删除回收站记录
				tx.delete(item);
//				deserializeObject(item, tx);
			}
			//关闭tx
			tx.commit();
			
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			if (tx != null) {
				//关闭tx
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		} finally {
		}
	}
	//1.这里反序列化可以缓存到memcached中2.做成工具方法
	private void deserializeObject(RTrash rTrash, TransactionCache tx) throws Exception {
		RTrashContent rTrashContent = QueryCache.get(RTrashContent.class, rTrash.getEntityId());
		Object entityObj = deserializeObject(rTrashContent.getEntityContent());
		if (entityObj != null) {
			tx.save(entityObj);
			tx.delete(rTrashContent);
			tx.delete(rTrash);
		} else {
			throw new ExceptionFactory().makeBaseException("sysmgrMsg.deserializeObjectException");
		}
	}
	
	/**
	 * 根据byte[] content内容还原实体对象
	 * @param content
	 * @return
	 */
	public Object deserializeObject(byte[] content) {
		Object entityObj = null;
		ByteArrayInputStream bais = null;
		ObjectInputStream ois = null;
		try {
			bais = new ByteArrayInputStream(content);
			ois = new ObjectInputStream(bais);
			entityObj = ois.readObject();
			ois.close();
			bais.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ois != null) {
					ois.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				entityObj = null;
			}
		}
		return entityObj;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
